package com.hs.srm.admin.modules.material.service.impl;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.material.dao.MaterialLeadDao;
import com.hs.srm.admin.modules.material.entity.MaterialFactoryEntity;
import com.hs.srm.admin.modules.material.entity.MaterialLeadEntity;
import com.hs.srm.admin.modules.material.service.MaterialFactoryService;
import com.hs.srm.admin.modules.material.service.MaterialLeadService;
import com.hs.srm.base.common.annotation.DataFilter;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * @author socik
 * @date 2019/5/28 14:10
 * @email socik@qq.com
 */
@Service("materialLeadService")
public class MaterialLeadServiceImpl extends ServiceImpl<MaterialLeadDao, MaterialLeadEntity> implements MaterialLeadService {
    @Autowired
    private MaterialFactoryService materialFactoryService;

    /**
     * @Description: 领用实现方法
     */
    @Override
    public boolean insertMaterialLead(MaterialLeadEntity materialLeadEntity) {
        boolean flg = this.insert(materialLeadEntity);
        return flg;
    }

    /**
     * @Description: 查询物料领用数据
     */
    @Override
    public MaterialLeadEntity selectEntityByCode(String code, String werks) {
        EntityWrapper<MaterialLeadEntity> queryWrapper = new EntityWrapper<>();
        queryWrapper.eq("material_code", code).eq("werks", werks).notIn("leadnum", 0);
        MaterialLeadEntity materialLeadEntity = this.selectOne(queryWrapper);
        return materialLeadEntity;
    }

    @Override
    public MaterialLeadEntity selectEntityByUser(String code, String werks, String username) {
        EntityWrapper<MaterialLeadEntity> queryWrapper = new EntityWrapper<>();
        queryWrapper.eq("material_code", code).eq("werks", werks).eq("account", username).eq("leadnum", 0);
        MaterialLeadEntity materialLeadEntity = this.selectOne(queryWrapper);
        return materialLeadEntity;
    }

    @Override
    public List<MaterialLeadEntity> selectEntityListByUser(String username) {
        EntityWrapper<MaterialLeadEntity> queryWrapper = new EntityWrapper<>();
        queryWrapper.eq("account", username).eq("leadnum", 0);
        List<MaterialLeadEntity> list = this.selectList(queryWrapper);
        return list;
    }

    @Override
    public boolean delateMaterial(String pkid) {
        return this.deleteById(pkid);
    }

    @Override
    @DataFilter
    public PageUtils queryPage(Map<String, Object> params) {
        String fhwc = (String) params.get("fhwc");
        String sql = "";
        if ("0".equals(fhwc)) {
            sql = "leadnum-returnnum=0";
        } else if ("1".equals(fhwc)) {
            sql = "leadnum-returnnum>0";
        } else {
            sql = "1=1";
        }
        Page<MaterialLeadEntity> page = this.selectPage(
                new QueryParam<MaterialLeadEntity>(params).getPage(),
                new EntityWrapper<MaterialLeadEntity>()
                        .like("material_code", String.valueOf(params.get("materialCode")))
                        .like("werks", String.valueOf(params.get("werks")), SqlLike.CUSTOM)
                        .like(StringUtils.isNotBlank(String.valueOf(params.get("leadMan"))), "leadman", String.valueOf(params.get("leadMan")))
                        .ge(StringUtils.isNotBlank(String.valueOf(params.get("leadS"))), "createtime", params.get("leadS")).le(StringUtils.isNotBlank(String.valueOf(params.get("leadE"))), "createtime", params.get("leadE"))
                        .notIn("leadnum", 0)
                        .orderBy("createtime", false)
                        .and(sql));
        List<MaterialLeadEntity> list = page.getRecords();
        for (MaterialLeadEntity entity : list) {
            MaterialFactoryEntity factoryEntity = this.materialFactoryService.selectMaterialFactoryByCode(entity.getMaterialCode(), entity.getWerks());
            entity.setMaterialDesc(factoryEntity.getMaterialDesc());
        }
        page.setRecords(list);
        return new PageUtils(page);
    }
}
